#include <src/pcps.h>

// declare global mutexes
namespace pcps {

  // use static so that only functions in this file can access them directly
  static pthread_mutex_t alloc_mutex_object;
  static pthread_mutex_t  rand_mutex_object;

}

//-------------------------------------------------------------------------------
// pcps::init_mutexes -- initializes the global mutexes
//-------------------------------------------------------------------------------

void pcps::init_mutexes() {
  pthread_mutex_init(&pcps::alloc_mutex_object, NULL);
  pthread_mutex_init(&pcps::rand_mutex_object, NULL);
}

//-------------------------------------------------------------------------------
// pcps::destroy_mutexes -- destroys the global mutexes
//-------------------------------------------------------------------------------

void pcps::destroy_mutexes() {
  pthread_mutex_destroy(&pcps::alloc_mutex_object);
  pthread_mutex_destroy(&pcps::rand_mutex_object);
}

//-------------------------------------------------------------------------------
// pcps::alloc_mutex -- returns a pointer to a mutex used by the Arena allocator
//                      to ensure that its allocation/deallocation is thread safe
//-------------------------------------------------------------------------------

pthread_mutex_t * pcps::alloc_mutex() { return &pcps::alloc_mutex_object; }

//-------------------------------------------------------------------------------
// pcps::rand_mutex -- returns a pointer to a mutex for ensuring that threads
//                     can safely use the global random number generator
//-------------------------------------------------------------------------------

pthread_mutex_t * pcps::rand_mutex() { return &pcps::rand_mutex_object; }
